import moment from 'moment/moment';

const tableOption = {
  border: true,
  addBtn: false,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  index: true,
  menu: false,
  columnBtn: false,
  refreshBtn: false,
  indexLabel: '序号',
  column: [
    {
      label: '日期',
      prop: 'date'
    },
    {
      label: '车间名称',
      prop: 'workshopName'
    },
    {
      label: '产品编码',
      prop: 'partNo'
    },
    {
      label: '产品名称',
      prop: 'partName',
      span: 7
    },
    {
      label: '产品型号',
      prop: 'partModel'
    },
    {
      label: '产量',
      prop: 'yield'
    }
  ]
};

const getOption = (self) => {
  return {
    menuPosition: 'right',
    submitBtn: false,
    emptyBtn: false,
    column: [
      {
        label: '统计维度',
        prop: 'statisticsTimeDimension',
        type: 'radio',
        button: true,
        span: 4,
        value: 'DAY',
        dicData: [
          {
            label: '日',
            value: 'DAY'
          },
          {
            label: '周',
            value: 'WEEK'
          },
          {
            label: '月',
            value: 'MONTH'
          }
        ],
        control: (val, form) => {
          if (form.statisticsTimeDimension === 'DAY') {
            return {
              examineDate: {
                display: true
              },
              weekStart: {
                display: false
              },
              weekEnd: {
                display: false
              },
              month: {
                display: false
              }
            };
          } else if (form.statisticsTimeDimension === 'WEEK') {
            return {
              weekStart: {
                display: true
              },
              weekEnd: {
                display: true
              },
              examineDate: {
                display: false
              },
              month: {
                display: false
              }
            };
          } else {
            return {
              examineDate: {
                display: false
              },
              weekStart: {
                display: false
              },
              weekEnd: {
                display: false
              },
              month: {
                display: true
              }
            };
          }
        }
      },
      {
        label: '时间范围',
        span: 6,
        prop: 'examineDate',
        type: 'daterange',
        format: 'yyyy-MM-dd',
        valueFormat: 'yyyy-MM-dd',
        startPlaceholder: '开始日期',
        endPlaceholder: '结束日期',
        rules: [
          {
            required: true,
            message: '请选择时间范围',
            trigger: 'blur'
          },
          {
            trigger: 'change',
            validator: (rule, value, callback) => {
              const endTime = value[1];
              const startTime = value[0];
              if (moment(endTime).diff(moment(startTime), 'day') >= 90) {
                callback(new Error('查询时间最多选择90天'));
              }
              callback();
            }
          }
        ]
      },
      {
        label: '起始周',
        span: 4,
        prop: 'weekStart',
        type: 'week',
        format: 'yyyy 第 WW 周',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择起始周范围',
            trigger: 'blur'
          }
        ]
      },
      {
        label: '结束周',
        span: 4,
        prop: 'weekEnd',
        type: 'week',
        format: 'yyyy 第 WW 周',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择结束周范围',
            trigger: 'blur'
          },
          {
            trigger: 'change',
            validator: self.validateWeek
          }
        ]
      },
      {
        label: '时间范围',
        span: 6,
        prop: 'month',
        type: 'monthrange',
        format: 'yyyy-MM',
        startPlaceholder: '开始月份',
        endPlaceholder: '结束月份',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择时间范围',
            trigger: 'blur'
          },
          { validator: self.validateDateRange, trigger: 'change' }
        ]
      },
      {
        formslot: true,
        prop: 'contain',
        span: 6
      },
      {
        label: '',
        prop: 'btn',
        span: 6,
        formslot: true
      }
    ]
  };
};

//不同表格的option
const getTableOption = (type) => {
  const columMap = {
    workshop: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '车间名称',
        prop: 'workshopName'
      },
      {
        label: '产品编码',
        prop: 'partNo'
      },
      {
        label: '产品名称',
        prop: 'partName',
        span: 7
      },
      {
        label: '产品型号',
        prop: 'partModel'
      },
      {
        label: '产量',
        prop: 'yield'
      },
      // {
      //   label: '报废数量',
      //   prop: 'scrapQuantity'
      // },
    ],
    product: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '产品编码',
        prop: 'partNo'
      },
      {
        label: '产品名称',
        prop: 'partName',
        span: 7
      },
      {
        label: '产品型号',
        prop: 'partModel'
      },
      {
        label: '工单号',
        prop: 'workOrderNo'
      },
      {
        label: '产量',
        prop: 'yield'
      },
      // {
      //   label: '报废数量',
      //   prop: 'scrapQuantity'
      // },
    ],
    device: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '设备编码',
        prop: 'equipmentCode'
      },
      {
        label: '设备名称',
        prop: 'equipmentName'
      },
      // {
      //   label: '设备品牌',
      //   prop: 'equipmentBrand'
      // },
      {
        label: '可动率',
        prop: 'inspectionNgRate'
      },
      {
        label: '车间',
        prop: 'workshopName'
      },
      {
        label: '产线',
        prop: 'lineName'
      },
      {
        label: '产品编码',
        prop: 'partNo'
      },
      {
        label: '工单',
        prop: 'workOrderNo'
      },
      {
        label: '产品名称',
        prop: 'partName',
        span: 7
      },
      {
        label: '产品型号',
        prop: 'partModel'
      },
      {
        label: '产量',
        prop: 'yield'
      },
      // {
      //   label: '报废数量',
      //   prop: 'scrapQuantity'
      // },
    ],
    process: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '工序',
        prop: 'routeName'
      },
      {
        label: '产品编码',
        prop: 'partNo'
      },
      {
        label: '产品名称',
        prop: 'partName',
        span: 7
      },
      {
        label: '产品型号',
        prop: 'partModel'
      },
      {
        label: '产量',
        prop: 'yield'
      },
      // {
      //   label: '报废数量',
      //   prop: 'scrapQuantity'
      // },
    ],
    team: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '班组',
        prop: 'workshopName'
      },
      {
        label: '人数',
        prop: 'inspectionQuantity'
      },
      {
        label: '产线',
        prop: 'inspectionNgQuantity',
        span: 7
      },
      {
        label: '工单',
        prop: 'workOrderNo'
      },
      {
        label: '产品',
        prop: 'inspectionNgRate'
      },
      {
        label: '产量',
        prop: 'workshopPpmTargetValue'
      },
      // {
      //   label: '报废数量',
      //   prop: 'scrapQuantity'
      // },
    ],
    personnel: [
      {
        label: '日期',
        prop: 'date'
      },
      {
        label: '人员',
        prop: 'personnel'
      },
      {
        label: '工单',
        prop: 'workOrderNo',
        span: 7
      },
      {
        label: '产品编码',
        prop: 'partNo'
      },
      {
        label: '产品名称',
        prop: 'partName',
        span: 7
      },
      {
        label: '产品型号',
        prop: 'partModel'
      },
      {
        label: '工序',
        prop: 'routeName'
      },
      {
        label: '产量',
        prop: 'workshopPpmTargetValue'
      },
      {
        label: '报废数量',
        prop: 'scrapQuantity'
      },
    ]
  };
  return columMap[type];
};

// 不同维度的标识参数
const getStatisticType = (type) => {
  const map = {
    workshop: 'WORK_SHOP',
    product: 'PRODUCT',
    device: 'EQUIPMENT',
    process: 'ROUTE',
    team: 'SHIFT_GROUP',
    personnel: 'PERSONNEL'
  };
  return map[type];
};
const getStartDay = (time, type) => {
  if (type === 'DAY') {
    return moment(time).format('yyyy-MM-DD');
  } else if (type === 'WEEK') {
    return moment(time).startOf('isoWeek').format('yyyy-MM-DD');
  } else {
    return moment(time).startOf('month').format('yyyy-MM-DD');
  }
};

const getEndDay = (time, type) => {
  if (type === 'DAY') {
    return moment(time).format('yyyy-MM-DD');
  } else if (type === 'WEEK') {
    return moment(time).endOf('isoWeek').format('yyyy-MM-DD');
  } else {
    return moment(time).endOf('isoWeek').format('yyyy-MM-DD');
  }
};

const valiDateFlied = (from, type) => {
  const validateMap = {
    workshop: 'workshopId',
    product: 'partMasterId',
    device: 'equipmentId',
    process: 'routeName',
    team: 'team',
    personnel: 'personnel'
  };
  const field = validateMap[type];
  return from[field] && from[field].length;
};
export { tableOption, getOption, getStatisticType, getStartDay, getEndDay, getTableOption, valiDateFlied };
